package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.db.cache.YhCache;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.db.cache.YhxddxCache;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 用户寻敌对象
 */
public class Yhxddx {

	public static final String TABLENAME = "用户寻敌对象";
	public static final String CLASSNAME = "Yhxddx"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** 用户id */
	private int yhid;
	
	/** 目标用户id */
	private int mbyhid;
	
	/** 有效 */
	private boolean yx;
	
	
	public Yhxddx() {
		super();
	}
	
	public Yhxddx(int id, int yhid, int mbyhid, boolean yx) {
		super();
		this.id = id;
		this.yhid = yhid;
		this.mbyhid = mbyhid;
		this.yx = yx;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getYhid() {
		return yhid;
	}
	
	public void setYhid(int yhid) {
		this.yhid = yhid;
		addUpdateColumn("用户id",yhid);
	}
	
	public void changeYhidWith(int yhid){
		this.yhid += yhid;
		setYhid(this.yhid);
	}
	
	public void changeYhidWithMin(int yhid,int min){
		this.yhid += yhid;
		this.yhid = this.yhid < min ? min : this.yhid;
		setYhid(this.yhid);
	}
	
	public void changeYhidWithMax(int yhid,int max){
		this.yhid += yhid;
		this.yhid = this.yhid > max ? max : this.yhid;
		setYhid(this.yhid);
	}
	
	public void changeYhidWithMaxMin(int yhid,int max,int min){
		this.yhid += yhid;
		this.yhid = this.yhid < min ? min : this.yhid;
		this.yhid = this.yhid > max ? max : this.yhid;
		setYhid(this.yhid);
	}	
	public int getMbyhid() {
		return mbyhid;
	}
	
	public void setMbyhid(int mbyhid) {
		this.mbyhid = mbyhid;
		addUpdateColumn("目标用户id",mbyhid);
	}
	
	public void changeMbyhidWith(int mbyhid){
		this.mbyhid += mbyhid;
		setMbyhid(this.mbyhid);
	}
	
	public void changeMbyhidWithMin(int mbyhid,int min){
		this.mbyhid += mbyhid;
		this.mbyhid = this.mbyhid < min ? min : this.mbyhid;
		setMbyhid(this.mbyhid);
	}
	
	public void changeMbyhidWithMax(int mbyhid,int max){
		this.mbyhid += mbyhid;
		this.mbyhid = this.mbyhid > max ? max : this.mbyhid;
		setMbyhid(this.mbyhid);
	}
	
	public void changeMbyhidWithMaxMin(int mbyhid,int max,int min){
		this.mbyhid += mbyhid;
		this.mbyhid = this.mbyhid < min ? min : this.mbyhid;
		this.mbyhid = this.mbyhid > max ? max : this.mbyhid;
		setMbyhid(this.mbyhid);
	}	
	public boolean getYx() {
		return yx;
	}
	
	public void setYx(boolean yx) {
		this.yx = yx;
		addUpdateColumn("有效",yx);
	}
	
	
	//用户id
	public Yh getYhPkYhid(){
		return YhCache.getById(yhid);
	}
	//目标用户id
	public Yh getYhPkMbyhid(){
		return YhCache.getById(mbyhid);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("yhid", this.yhid);
        result.put("mbyhid", this.mbyhid);
        result.put("yx", this.yx);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("用户id", this.yhid);
        result.put("目标用户id", this.mbyhid);
        result.put("有效", this.yx);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.yhid);
		    SK_OutputStream.writeInt(out,this.mbyhid);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Yhxddx createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Yhxddx yhxddx = new Yhxddx();
		    yhxddx.id = SK_InputStream.readInt(in,null);
		    yhxddx.yhid = SK_InputStream.readInt(in,null);
		    yhxddx.mbyhid = SK_InputStream.readInt(in,null);
		    return yhxddx;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhxddx> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yhxddx> yhxddxs = new ArrayList<Yhxddx>();
		for (Map<String, Object> map : list) {
			yhxddxs.add(createForColumnNameMap(map));
		}
		return yhxddxs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhxddx createForColumnNameMap(Map<String, Object> map){
    	Yhxddx obj = new Yhxddx();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yhid = SK_Map.getInt("用户id", map);
	    obj.mbyhid = SK_Map.getInt("目标用户id", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhxddx> createForList(List<Map<String, Object>> list){
    	List<Yhxddx> yhxddxs = new ArrayList<Yhxddx>();
		for (Map<String, Object> map : list) {
			yhxddxs.add(createForColumnNameMap(map));
		}
		return yhxddxs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhxddx createForMap(Map<String, Object> map){
    	Yhxddx obj = new Yhxddx();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yhid = SK_Map.getInt("yhid", map);
	    obj.mbyhid = SK_Map.getInt("mbyhid", map);
        return obj;
    }
    
    public static Yhxddx createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yhxddx> createForJson(List<String> jsons){
    	List<Yhxddx> yhxddxs = new ArrayList<Yhxddx>();
    	for(String json : jsons){
    		yhxddxs.add(createForJson(json));
    	}
    	return yhxddxs;
    }
    
    /** 延迟插入数据库 */
    public Yhxddx insert(){
    	return YhxddxCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Yhxddx update(){
    	return YhxddxCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YhxddxCache.delete(this);
    }
    /** 即时插入数据库 */
    public Yhxddx insertAndFlush(){
    	return YhxddxCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yhxddx updateAndFlush(){
    	return YhxddxCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YhxddxCache.deleteAndFlush(this);
    }
}